package model

import (
	"furni-suite/furnirpc/furnirpc"
	"gorm.io/gorm"
)

type Furni struct {
	gorm.Model
	FurniName  string  `gorm:"type:varchar(30)"`
	FurniPrice float64 `gorm:"type:decimal(10,2)"`
	Bid        int     `gorm:"type:int"`
	Cid        int     `gorm:"type:int"`
}

func (f *Furni) FurniAdd(db *gorm.DB) error {
	return db.Create(f).Error
}
func (f *Furni) FindFurniByName(db *gorm.DB, name string) error {
	return db.Where("furni_name=?", name).First(f).Error
}
func FindFurniById(db *gorm.DB, id int) []*furnirpc.FurniShow {
	var list []*furnirpc.FurniShow
	db.Model(&Furni{}).Select(
		"furnis.id as furni_id",
		"furnis.furni_name",
		"furnis.furni_price",
		"classes.class_name",
		"brands.brand_name",
	).
		Joins("LEFT JOIN classes ON furnis.cid = classes.id").
		Joins("LEFT JOIN brands ON furnis.bid = brands.id").
		Where("furnis.id=?", id).First(&list)
	return list
}

type Class struct {
	gorm.Model
	ClassName string `gorm:"type:varchar(30)"`
}

func (c *Class) FindClassByName(db *gorm.DB, name string) error {
	return db.Where("class_name=?", name).FirstOrCreate(c).Error
}

type Brand struct {
	gorm.Model
	BrandName string `gorm:"type:varchar(30)"`
}

func (b *Brand) FindBrandByName(db *gorm.DB, name string) error {
	return db.Where("brand_name=?", name).FirstOrCreate(b).Error
}

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"`
	Password string `gorm:"type:varchar(32)"`
}

func (u *User) UserShow(db *gorm.DB, id int) error {
	return db.Where("id =?", id).First(u).Error

}
